package com.ydlclass;

import com.ydlclass.utils.zookeeper.ZookeeperNode;
import com.ydlclass.utils.zookeeper.ZookeeperUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.zookeeper.*;

import java.util.List;

/**
 * 注册中心的管理页面
 * @Author duWenJian
 * @Date 2024/2/12 13:45
 * @Version 1.0
 */
@Slf4j
public class Application {
    public static void main(String[] args) {
        // 帮我们创建基础目录
        /*
            yrpc-metadata (持久节点由menage进行创建)
        └─ providers (持久节点)
            └─ service1 (持久节点，接口的全限定名)
                ├─ node1 [data]    /ip:port
                ├─ node2 [data]
                ├─ node3 [data]
        └─ consumers
            └─ service1
                ├─ node1 [data]    /ip:port
                ├─ node2 [data]
                ├─ node3 [data]
        └─ configs
         */
            ZooKeeper zooKeeper = ZookeeperUtils.createZookeeper();
            // 定义节点和数据
            String basePath = Constant.BASE_PATH;
            String providersPath = Constant.BASE_PROVIDERS_PATH;
            String consumersPath = Constant.BASE_CONSUMERS_PATH;

            ZookeeperNode baseNode = new ZookeeperNode(basePath, null);
            ZookeeperNode providersNode = new ZookeeperNode(providersPath, null);
            ZookeeperNode consumersNode = new ZookeeperNode(consumersPath, null);

            // 创建节点
            List.of(baseNode, providersNode, consumersNode).forEach(node -> {
                ZookeeperUtils.createNode(zooKeeper, node, null, CreateMode.PERSISTENT);
            });
            ZookeeperUtils.shutDownZookeeper(zooKeeper);
    }
}
